﻿using Microsoft.VisualStudio.Modeling;
using System;


namespace BizAppFactory.Domain
{
    partial class FixUpDiagram
    {
        private ModelElement GetParentForEntity(Entity entity)
        {
            if (entity.BizAppDomainModel != null)
            {
                return entity.BizAppDomainModel;
            }
            else if (entity.Aggregate != null)
            {
                return entity.Aggregate;
            }
            else if (entity.Module!= null)
            {
                return entity.Module;
            }
            else if (entity.CRUDApplicationService != null)
            {
                return entity.CRUDApplicationService;
            }

            throw new ApplicationException("No parent was set for entity!");
        }

        private ModelElement GetParentForValueObject(ValueObject valueObject)
        {
            if (valueObject.BizAppDomainModel != null)
            {
                return valueObject.BizAppDomainModel;
            }
            else if (valueObject.Aggregate != null)
            {
                return valueObject.Aggregate;
            }
            else if (valueObject.Module != null)
            {
                return valueObject.Module;
            }
            else if (valueObject.CRUDApplicationService != null)
            {
                return valueObject.CRUDApplicationService;
            }

            throw new ApplicationException("No parent was set for entity!");
        }

        private ModelElement GetParentForListOfValues(ListOfValues listOfValues)
        {
            if (listOfValues.BizAppDomainModel != null)
            {
                return listOfValues.BizAppDomainModel;
            }
            else if (listOfValues.Aggregate != null)
            {
                return listOfValues.Aggregate;
            }
            else if (listOfValues.Module != null)
            {
                return listOfValues.Module;
            }
            else if (listOfValues.CRUDApplicationService != null)
            {
                return listOfValues.CRUDApplicationService;
            }

            throw new ApplicationException("No parent was set for entity!");
        }

        private ModelElement GetParentForDomainService(DomainService domainService)
        {
            if (domainService.BizAppDomainModel != null)
            {
                return domainService.BizAppDomainModel;
            }
            else if (domainService.Module != null)
            {
                return domainService.Module;
            }

            throw new ApplicationException("No parent was set for domain service!");
        }

        private ModelElement GetParentForRepository(Repository repository)
        {
            if (repository.BizAppDomainModel != null)
            {
                return repository.BizAppDomainModel;
            }
            else if (repository.Module != null)
            {
                return repository.Module;
            }

            throw new ApplicationException("No parent was set for domain service!");
        }

        private ModelElement GetParentForAggregate(Aggregate aggregate)
        {
            if (aggregate.BizAppDomainModel != null)
            {
                return aggregate.BizAppDomainModel;
            }
            else if (aggregate.Module != null)
            {
                return aggregate.Module;
            }
            else if (aggregate.CRUDApplicationService != null)
            {
                return aggregate.CRUDApplicationService;
            }

            throw new ApplicationException("No parent was set for entity!");
        }

        private ModelElement GetParentForCRUDApplicationService(CRUDApplicationService pCRUDApplicationService)
        {
            if (pCRUDApplicationService.BizAppDomainModel != null)
            {
                return pCRUDApplicationService.BizAppDomainModel;
            }
            else if (pCRUDApplicationService.Module != null)
            {
                return pCRUDApplicationService.Module;
            }

            throw new ApplicationException("No parent was set for entity!");
        }

        private ModelElement GetParentForModule(Module module)
        {
            if (module.BizAppDomainModel != null)
            {
                return module.BizAppDomainModel;
            }
            else if (module.ParentModule != null)
            {
                return module.ParentModule;
            }

            throw new ApplicationException("No parent was set for entity!");
        }
    }
}